/**
 * 引导类
 * 作用：初始化basePath，config和EToken信息，并引入公共文件
 * 启动入口：init
 * @author: zh.du
 */

var basePath = "";
var config = sessionStorage.getItem("config");
var request = false;
var layer;
//获取HTML中的参数
function requestParam(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return decodeURIComponent(r[2]);
	return null;
}

//初始化basePath
function initBasePath(){
	basePath = location.protocol + "//" + location.host + location.pathname.substr(0, location.pathname.indexOf("panda"));
	if (basePath.lastIndexOf("/") != basePath.length - 1) {
		basePath += "/";
	}
	var d = location.pathname.length - "index.html".length;
	if (d >= 0 && location.pathname.lastIndexOf("index.html") == d) {
		basePath = location.protocol + "//" + location.host + location.pathname.substr(0, location.pathname.indexOf("index.html"));
	}
}

//初始化ETtoken
function initETtoken(){
	var paramAry = location.hash.substr(1).split("|");
	paramAry[0] && sessionStorage.setItem("ETtoken", paramAry[0]);
	if (paramAry == "" && (location + "").indexOf("ETtoken") != -1) {
		sessionStorage.setItem("ETtoken", requestParam("ETtoken"));
	}
}

//网络请求方法
function sendHttpRequest(method, url, content, callback) {
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
		if (request.overrideMimeType) 
			request.overrideMimeType("text/xml");
	} else if (window.ActiveXObject) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				
			}
		}
	}
	request.onreadystatechange = callback;
	request.open(method, url, false);
	request.send(content);
}

//引入公共文件
function includeFile(){
	sendHttpRequest("get", basePath + "panda/include/include.html", "", function(){
		if (request.readyState == 4) {
			if (request.status == 200) {
				document.write(request.responseText.replace(/\$\{basePath\}/g, basePath));
			}
		}
	});
}

//获取配置信息
function getConfig(){
	if (request.readyState == 4) {//
		if (request.status == 200) {//
			var json = JSON.parse(request.responseText);
			var data = JSON.parse(json.data);
			if (data.servicePath == "") {
				data.servicePath = config.servicePath;
			}
			config = data;
			sessionStorage.setItem("config", JSON.stringify(data));
		}
	}
}

//引入config内容
function initConfig(){
	if (config) {
		config = JSON.parse(config);
	} else {
		sendHttpRequest("get", basePath + "config.json", "", function(){
			if (request.readyState == 4) {//
				if (request.status == 200) {//
					config = JSON.parse(request.responseText);
					if (!config.servicePath) {
						config.servicePath = basePath;
					}
					sessionStorage.setItem("config", JSON.stringify(config));
					//暂时注释掉
					//sendHttpRequest("get", config.servicePath + "common/config/getConfig", "", getConfig);//具体内容查看配置中心文件
				}
			}
		});
	}
}

//初始化变量
function initVariable(){
	
	initBasePath();
	
	initETtoken();
}

//初始化，入口方法
function init(){
	
	initVariable();//part1，初始化变量
	
	includeFile();//part2，引入公共文件
	
	initConfig();//part3，初始化config
	
}
init();//启动
